package com.android.net

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.IBinder
import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Route
import com.android.net.listener.OnAllDownloadsCompleteListener
import com.android.net.service.DownloadBinder
import com.android.net.service.DownloadService
import com.camera.base.bean.DownloadState
import com.camera.base.inerface.DownloadCallback
import com.camera.routemodule.RouterPath
import com.camera.routemodule.routeinterface.DownLoadInterface

// 定义回调接口
@Route(path = RouterPath.path.network_dowload)
class DownloadImpl: DownLoadInterface,OnAllDownloadsCompleteListener {
    val TAG = DownloadImpl::class.java.simpleName
    var context : Context? = null
    var mDownloadBinder: DownloadBinder? = null
    val cacheDownloadList: MutableSet<Triple<String,String,DownloadCallback>> = mutableSetOf()
    private val mServiceConnection: ServiceConnection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName, service: IBinder) {
            Log.e(TAG, "onServiceConnected: "+cacheDownloadList.isNotEmpty() )
            mDownloadBinder = service as DownloadBinder
            mDownloadBinder?.init(context)
            mDownloadBinder!!.setOnAllDownloadsCompleteListener(this@DownloadImpl)
            if (cacheDownloadList.isNotEmpty()){
                Log.e(TAG, "onServiceConnected: "+cacheDownloadList.size )
                if (cacheDownloadList.size > 0){
                    cacheDownloadList.forEach {
                        Log.e(TAG, "startDownload:  2 " )
                        mDownloadBinder!!.startDownload(it.first,it.second,it.third)
                    }
                    cacheDownloadList.clear()
                }
            }
        }

        override fun onServiceDisconnected(name: ComponentName) {
            mDownloadBinder = null
        }
    }

    override fun startDownload(downloadUrl: String, localPath: String, callback: DownloadCallback) {
        if (mDownloadBinder == null){
            cacheDownloadList.add(Triple(downloadUrl,localPath,callback))
        }else{
            mDownloadBinder!!.startDownload(downloadUrl,localPath,callback)
        }
    }

    override fun pauseDownload(fileName: String) {
        mDownloadBinder?.pauseDownload(fileName)
    }

    override fun resumeDownload(fileName: String) {
        mDownloadBinder?.resumeDownload(fileName)
    }

    override fun cancelDownload(fileName: String) {
        mDownloadBinder?.cancelDownload(fileName)
    }

    override fun getDownloadStateByPath(filePath: String): DownloadState? {
        return mDownloadBinder?.getDownloadStateByPath(filePath)
    }

    override fun init(context: Context?) {
        Log.e(TAG, "init: " )
        this.context = context
        if (mDownloadBinder == null){
            Log.e(TAG, "init: 111" )
            context?.bindService(Intent(context, DownloadService::class.java),mServiceConnection,Context.BIND_AUTO_CREATE)
        }
    }

    override fun onAllDownloadsComplete() {
//        if (mDownloadBinder != null){
//            context?.unbindService(mServiceConnection)
//        }
    }


}
